extends ../layout

block meta
    link(rel="stylesheet", href="#{options.cdn_path}/lib/artDialog/skins/default.css?v=#{options.site_version}")
    include ../common/_kindeditor

block content
    // 传递参数
    dashboardClass = true
    tab = blog.id ? '修改博文' : '写博'
    include ../common/_admin_navbar
    .content
        .header
            h1.page-title #{tab}
        ul.breadcrumb
            li
                a(href="/admin") 首页
                span.divider /
            li.active #{tab}
        .container-fluid
            .row-fluid
                form(action="/admin_blog/save_update", method="post")
                    input(type="hidden", name="blog.id", value="#{blog.id}")
                    .btn-toolbar
                        button.btn.btn-primary(type="submit")
                            i.icon-save 
                            | &nbsp;保存
                        a.btn(href="/admin_blog/list") 返回
                        .btn-group
                    .well
                        #myTabContent.tab-content
                            label 标题
                            input.input-xlarge(type="text", name="blog.title", value="#{blog.title}", required="required")
                            label 分类
                            select#DropDownTimezone.input-xlarge(name="blog.blog_type")
                                types = [{'id': 0, 'type_name': '原创'}, {'id': 1, 'type_name': '收藏'}]
                                each item in types
                                    if item.id == blog.blog_type
                                        option(value="#{item.id}", selected="selected") #{item.type_name}
                                    else
                                        option(value="#{item.id}") #{item.type_name}
                            label 标签
                            input#textarea.input-xxlarge(type="text")
                            label 来源
                            input.input-xxlarge(type="text", name="blog.share_url", value="#{blog.share_url}")
                            label 内容
                            textarea#editor.input-xlarge(name="blog.content") !{blog.content}

                include ../common/_admin_footer

block script
    script(src="#{options.cdn_path}/lib/artDialog/jquery.artDialog.js?v=#{options.site_version}")
    script(src="#{options.cdn_path}/lib/textext/textextjs.min.js?v=#{options.site_version}")
    script
        $(function(){
            $('#textarea').textext({
                plugins : 'tags prompt focus autocomplete ajax arrow',
                prompt : '添加...',
                ajax : {
                    url : '/admin_tags/all',
                    dataType : 'json',
                    cacheResults : true
                }
            });

            $('form').submit(function(){
                var dialog = art.dialog();
                var title = $('input[name="blog.title"]').val();
                var content = $('#editor').val();
                if( title.length < 3 ){
                    dialog.content('标题太短！').time(1);
                    return false;
                }
                if( content.length < 10 ){
                    dialog.content('亲再多些点吧！').time(1);
                    return false;
                }
                $.post($(this).attr('action')
                    , $('form').serialize()
                    , function(data){
                        if(data.state){
                            dialog.content('保存成功！').time(1);
                            setTimeout(function(){
                                    location.href = '/admin_blog/list';
                            }, 1500);
                        }else{
                            dialog.content('服务器忙，请稍候！').time(2);
                        }
                    }
                );
                return false;
            });
        });